﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Model;
using BLL;
using Newtonsoft.Json;

namespace TrainManage.Controllers
{
    public class RepairsController : Controller
    {
        WagonBll wagonbll = new WagonBll();
        RailWayBll railwayBll = new RailWayBll();
        PartsBll partsbll = new PartsBll();
        RepairsBll repairbll = new RepairsBll();
        RepairsBll repairsbll = new RepairsBll();
        //
        // GET: /Repairs/

        #region   添加报修

      
        /// <summary>
        /// 添加报修
        /// </summary>
        /// <returns></returns>
        public ActionResult AddRepair()
        {
            return View();
        }


        /// <summary>
        /// 父级节点
        /// </summary>
        /// <returns></returns>

        public string Partents()
        {
            List<Wagon> list = wagonbll.Show();
            return JsonConvert.SerializeObject(list);
        }

        /// <summary>
        /// 子级节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public string Partent_Id(int id)
        {
            var result = railwayBll.Shows().Where(m => m.WagonId == id).ToList();
            return JsonConvert.SerializeObject(result);
        }
        /// <summary>
        /// 三级级节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public string Partent_Id2(int id)
        {
            var result = partsbll.Shows().Where(m => m.RailWayId == id).ToList();
            return JsonConvert.SerializeObject(result);
        }


        /// <summary>
        /// 反填车厢的名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string BackCXName(int id)
        {
            var result = railwayBll.Back(id);
            //获取车号id
            var cid = result.WagonId;
            var chehao = wagonbll.Back(cid);
            var c=chehao.WagonName;
            Session["chName"] = c;
            //获取车厢名称
            var name = result.RailWayName;
            Session["cXName"] = name;
            return name;
        }
         /// <summary>
         /// 提交
         /// </summary>
         /// <returns></returns>
        public ActionResult submints(HttpPostedFileBase image)
        {
            
            if (image != null)
            {
                Repairs r = new Repairs();
                r.RepairsName = Request["txtRepName"];
                r.RepairsDesc = Request["txtDesc"];
                r.FindPeople = Request["txtFname"];
                r.FindDate = Request["txtDate"].ToString();
                r.Grade = Request["txtGrade"];
                r.WangonName = Session["chName"].ToString();
                r.RailWayName = Session["cXName"].ToString();
                r.MaintainState = 0;
                r.ExamineState = 0;
                r.RapairsPeople = Session["UserName"].ToString();
                r.star = 0;
                r.comments = " ";
                r.choice = "暂无意见";
               
               //添加图片
               //截取文件字符串
                string imName = r.RepairsName + image.FileName.Substring(image.FileName.LastIndexOf('.'));
                 //将文件保存道服务端
                image.SaveAs(Server.MapPath("/phone/" + imName));
                r.RapairsPicture = "/phone/" + imName;
                var result = repairbll.Add(r);
                if (result)
                {
                    return Content("<script>alert('报修成功');location.href='/Repairs/AddRepair'</script>");
                }
                else
                {
                    return Content("<script>alert('报修失败');location.href='/Repairs/AddRepair'</script>");
                }

            }
            else
                return Content("<script>alert('请选择文件');location.href='/Repairs/AddRepair'</script>");
        }

        #endregion


        #region  显示报修
        /// <summary>
        /// 我的报修
        /// </summary>
        /// <returns></returns>
        public ActionResult RepairManage(int PageIndex=1)
        {
            int PageSize = 2;
            int PageCount = 0;
            var name = Request["txtName"];
            var date =  Request["txtdate"];
            var state = Request["txtState"];
            var LogName = Session["UserName"].ToString();
            var result = repairsbll.show(name, date, state, LogName,PageIndex,PageSize,out PageCount);
            ViewBag.list = result;
            ViewBag.TotalPage = (int)Math.Ceiling((double)PageCount / (double)PageSize);
            ViewBag.PageIndex = PageIndex;
            ViewBag.PageSize = PageSize;
            return View();
        }


        #endregion


        #region  我的评价
        /// <summary>
        /// 我的评价
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowAppraise(int id)
        {
            var result = repairbll.Back(id);
            return View(result);
        }

         /// <summary>
         /// 评价
         /// </summary>
         /// <returns></returns>
        public bool pingjia()
        {
           
            MaintainBll mabll= new MaintainBll();
            var star = float.Parse(Request["star"]);
            var comments = Request["comments"];
            var id = Convert.ToInt32(Request["id"]);
            var result = repairbll.updping(star, comments, id);
            Maintain list = mabll.BackMaintain(id);
            if(result>0)
            {
                list.PingXing = star;
              if(mabll.UpdateMain(list, list.RepairsId))
              {
                  return true;
              }
              else
              {
                  return false;
              }
               
            }
            return false;
        }
        #endregion

    }
}
